package com.miplus.ccrm.core.service.strategy;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.strategy.StrategyEditVo;
import com.miplus.ccrm.core.controller.vo.strategy.StrategyReqVo;
import com.miplus.ccrm.core.controller.vo.strategy.StrategyResVo;
import com.miplus.ccrm.core.pojo.CpStrategy;
import com.miplus.ccrm.core.mapper.CpStrategyMapper;
import com.miplus.ccrm.core.pojo.FiTask;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author pc
* @description 针对表【cp_strategy(活动策略表)】的数据库操作Service实现
* @createDate 2024-03-21 18:26:27
*/
@Service
public class CpStrategyServiceImpl extends MPJBaseServiceImpl<CpStrategyMapper, CpStrategy>
    implements CpStrategyService{

    @Override
    public IPage<StrategyResVo> strategyList(StrategyReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                StrategyResVo.class,
                new MPJLambdaWrapper<CpStrategy>()
                        .selectAs("(select n.name from cp_calllist n where n.clid=t.clid)",StrategyResVo::getCliddsc)
                        .selectAll(CpStrategy.class)
                        .eq(paras.getClid()!=null,"clid",paras.getClid())
                        .orderByAsc("t.rank")
        );
    }
    @Override
    public CmdResVo strategyUpdate(StrategyEditVo paras)
    {
        CpStrategy def=new CpStrategy();
        //for TDSQL,2024-07-29
        //BeanUtils.copyProperties(paras,def);
        def.setCcc(paras.getCcc());
        def.setClid(paras.getClid());
        def.setIsrpc(paras.getIsrpc());
        def.setHelpstr(paras.getHelpstr());
        def.setIsfinish(paras.getIsfinish());
        def.setRetryspan(paras.getRetryspan());
        def.setRetrytimes(paras.getRetrytimes());
        def.setStrategyid(paras.getStrategyid());
        def.setRank(paras.getRank());
        int ret=baseMapper.updateById(def);
        return new CmdResVo(ret,def.getStrategyid().toString());
    }

    @Override
    public CmdResVo strategyInsert(StrategyEditVo paras)
    {
        CpStrategy def=new CpStrategy();
        //for TDSQL,2024-07-29
        //BeanUtils.copyProperties(paras,def);
        def.setCcc(paras.getCcc());
        def.setClid(paras.getClid());
        def.setIsrpc(paras.getIsrpc());
        def.setHelpstr(paras.getHelpstr());
        def.setIsfinish(paras.getIsfinish());
        def.setRetryspan(paras.getRetryspan());
        def.setRetrytimes(paras.getRetrytimes());

        int ret=baseMapper.insert(def);

        baseMapper.updateRank(def.getStrategyid(),paras.getRank());

        return new CmdResVo(ret,def.getStrategyid().toString());
    }

    @Override
    public CmdResVo strategyDelete(int id)
    {
        int ret=baseMapper.deleteById(id);
        return new CmdResVo(ret,Integer.toString(id));
    }

    @Override
    public List<LabelResVo> contactResults(String clid)
    {
        return selectJoinList(LabelResVo.class,
                new MPJLambdaWrapper<CpStrategy>()
                        .selectAs("ccc",LabelResVo::getValue)
                        .selectAs("helpstr",LabelResVo::getLabel)
                        .select("ccc","helpstr")
                        .eq(CpStrategy::getClid,clid)
                        .orderByAsc(CpStrategy::getRank)
        );
    }

    @Override
    public String getTaskStatus(String clid,String result)
    {
        String ret="O";
        CpStrategy cpStrategy=selectJoinOne(CpStrategy.class,
                new MPJLambdaWrapper<CpStrategy>()
                        .selectAll(CpStrategy.class)
                        .eq(CpStrategy::getClid,clid)
                        .eq(CpStrategy::getCcc,result)
        );
        if(cpStrategy==null){
            ret="F";
        }
        else if(cpStrategy.getIsrpc()==1){
            ret="R";
        }
        else if(cpStrategy.getIsfinish()==1){
            ret="F";
        }

        return ret;
    }

    @Override
    public String getCcc(String clid,String result)
    {
        CpStrategy cpStrategy=selectJoinOne(CpStrategy.class,
                new MPJLambdaWrapper<CpStrategy>()
                        .selectAll(CpStrategy.class)
                        .eq(CpStrategy::getClid,clid)
                        .eq(CpStrategy::getHelpstr,result)
        );
        if(cpStrategy==null) return "";
        return cpStrategy.getCcc();
    }
}




